From 26b68e698bfb6c77a9a727c5167edf3eed761cf3 Mon Sep 17 00:00:00 2001 From: robertlipe Date: Mon, 25 Mar 2013 01:18:38 +0000 Subject: [PATCH] QXmlStreamWriter was fun. Let's try QXmlStreamReader. Actually, for a simple format like "geo", it plays out quite nicely. git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@4364 f51c46e8-681c-474f-0cfe-069cfd0219fb --- gpsbabel/geo.cc | 137 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 2 deletions(-) diff --git a/gpsbabel/geo.cc b/gpsbabel/geo.cc index 09428d12e..f77136f28 100644 --- a/gpsbabel/geo.cc +++ b/gpsbabel/geo.cc @@ -22,10 +22,11 @@ static char* deficon = NULL; static char* nuke_placer; -static waypoint* wpt_tmp; static gbfile* ofd; +#include +#include #include #include QString ostring; @@ -39,6 +40,138 @@ arglist_t geo_args[] = { }; #define MYNAME "geo" + +#define NEWREADER 0 + +#if NEWREADER + +// This really should be class-local... +QXmlStreamReader reader; +QString geo_read_fname; + +geocache_container wpt_container(const QString&); + +// Compensate for most of class waypt still using C strings and needing +// copies anyway. +char * ShimString(QString s) +{ + return xstrdup(s.toUtf8().data()); +} + +double ShimAttributeDouble(QXmlStreamAttributes a, QString v) +{ + QString rv = a.value(v).toString(); + return rv.toDouble(); +} + +void GeoReadLoc() +{ + waypoint* wpt = NULL; + while (reader.tokenType() != QXmlStreamReader::EndDocument) { + if (reader.tokenType()==QXmlStreamReader::StartElement) { + if (reader.name() == "waypoint") { + wpt = waypt_new(); + waypt_alloc_gc_data(wpt); + // There is no 'unknown' alt value and so many reference files have + // leaked it that we just paper over that here. + wpt->altitude = 0; + } + if (reader.name() == "name") { + QXmlStreamAttributes a = reader.attributes(); + wpt->shortname = ShimString(a.value("id").toString()); + wpt->description = ShimString(reader.readElementText()); + } + if (reader.name() == "coord") { + QXmlStreamAttributes a = reader.attributes(); + wpt->latitude = ShimAttributeDouble(a, "lat"); + wpt->longitude = ShimAttributeDouble(a, "lon"); + } + if (reader.name() == "type") { + wpt->icon_descr = ShimString(reader.readElementText()); + } + if (reader.name() == "link") { + QXmlStreamAttributes a = reader.attributes(); + wpt->url_link_text = a.value("text").toString(); + wpt->url = reader.readElementText(); + } + if (reader.name() == "difficulty") { + wpt->gc_data->diff = reader.readElementText().toInt() * 10; + } + if (reader.name() == "terrain") { + wpt->gc_data->terr = reader.readElementText().toInt() * 10; + } + if (reader.name() == "container") { + wpt->gc_data->container = wpt_container(reader.readElementText()); + } + } + + if (reader.tokenType() == QXmlStreamReader::EndElement) { + if (reader.name() == "waypoint") { + waypt_add(wpt); + } + } + + reader.readNext(); + } +} + +static void +geo_rd_init(const char* fname) +{ + geo_read_fname = fname; +} + +static void +geo_read(void) +{ + QFile file(geo_read_fname); + file.open(QIODevice::ReadOnly); + reader.setDevice(&file); + + while (!reader.atEnd()) { + if (reader.name() == "loc") { + GeoReadLoc(); + } + reader.readNextStartElement(); + } +} + +geocache_container wpt_container(const QString& args) +{ + geocache_container v; + + switch (args.toInt()) { + case 1: + v = gc_unknown; + break; + case 2: + v = gc_micro; + break; + case 3: + v = gc_regular; + break; + case 4: + v = gc_large; + break; + case 5: + v = gc_virtual;; + break; + case 6: + v = gc_other; + break; + case 8: + v = gc_small; + break; + default: + v = gc_unknown; + break; + } + return v; +} + + +#else +static waypoint* wpt_tmp; #define MY_CBUF 4096 #if ! HAVE_LIBEXPAT @@ -194,7 +327,7 @@ void wpt_container(const char* args, const char** unused) } waypt_alloc_gc_data(wpt_tmp)->container = v; } - +#endif void wpt_diff(const char* args, const char** unused) { if (!args) { -- 2.30.2